package com.wanho.java159.activemq.listenter;

import com.wanho.java159.cb.mapper.ItemMapper;
import com.wanho.java159.cb.po.Item;
import com.wanho.java159.common.util.FreeMarkerUtil;
import org.apache.commons.beanutils.BeanMap;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.Map;

public class ItemMessageListener implements MessageListener {
    @Resource
    private ItemMapper itemMapper ;
    @Resource
    private FreeMarkerUtil freeMarkerUtil ;

    @Override
    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            String text = "";
            try {
                text = ((TextMessage)message).getText();
            } catch (JMSException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //获得商品的id
            System.out.println("======ItemMessageListener=====onMessage===Item======id:"+text);
            //数据查询商品信息
            Item item = itemMapper.selectByPrimaryKey(Long.parseLong(text));
            //生成 商品静态页面
            //把bean转Map
            Map beanMap = new BeanMap(item);
            freeMarkerUtil.genHtml("shopdetail.ftl",beanMap);
        }
    }
}
